package xyz.kongbai121.demo.controller.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;
import xyz.kongbai121.demo.domain.RespBean;
import xyz.kongbai121.demo.util.UserPowerException;

/**
 * 统一异常处理
 * @author 空白
 */
@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler
    @ResponseBody
    public RespBean handleException(Exception e) {
        if (e instanceof UserPowerException) {
            System.out.println(e.getMessage());
            return new RespBean(false,e.getMessage());
        } else if (e instanceof NoHandlerFoundException) {
            System.out.println(e.getMessage());
            return new RespBean(false,"资源不存在");
        }else {
            e.printStackTrace();
            return new RespBean(false,"发生未知错误！");
        }
    }
}
